package com.wecon.springdemo.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Weikang  Wang
 * @program: com.wecon.springdemo.controller
 * @description: shiro测试控制器
 * @date 2020/4/28 13:27
 */
@RestController
@RequestMapping(value = "/shiro")
public class ShiroTestController {

    private Logger logger = LoggerFactory.getLogger(ShiroTestController.class);

    @RequestMapping(value = "/index")
    public ModelAndView testThymeleaf() {

        String name = "Weikang Wang";
        ModelAndView modelAndView = new ModelAndView("/index.html");
        modelAndView.addObject("name", name);

        return modelAndView;
    }

    @RequestMapping(value = "/add")
    public ModelAndView addUser(HttpServletResponse response) throws IOException {
        ModelAndView modelAndView = new ModelAndView("/user/adduser.html");
        return modelAndView;
    }

    @RequestMapping(value = "/update")
    public ModelAndView updateUser(HttpServletResponse response) throws IOException {
        ModelAndView modelAndView = new ModelAndView("/user/updateuser.html");
        return modelAndView;
    }

    @RequestMapping(value = "/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("/login.html");
        return modelAndView;
    }

    @RequestMapping(value = "/unAuth")
    public ModelAndView unAuth() {
        ModelAndView modelAndView = new ModelAndView("/user/unAuth.html");
        return modelAndView;
    }

    @RequestMapping(value = "/hello")
    public ModelAndView hello() {
        ModelAndView modelAndView = new ModelAndView("/user/hello.html");
        return modelAndView;
    }

    /**
     * 登录逻辑
     * @param name 用户名
     * @param password 密码
     * @return /
     */
    @RequestMapping(value = "/doLogin")
    public ModelAndView doLogin(String name, String password) {

        // 1.获取subject
        Subject subject = SecurityUtils.getSubject();
        // 2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        // 3.执行登录
        try {
            subject.login(token);
        } catch (AuthenticationException uae) {
            logger.error(uae.getMessage());
            return new ModelAndView("/login.html");
        }

        if (subject.isAuthenticated()) {
            logger.info("登录成功");
            ModelAndView modelAndView = new ModelAndView("/index.html");
            modelAndView.addObject("name", name);
            return modelAndView;
        } else {
            logger.info("登录失败");
            token.clear();
            return new ModelAndView("/login.html");
        }

    }

}
